package com.zdsoft.blp.zongbuju.viewpage.fragMent;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.application.MyApp;
import com.zdsoft.blp.gongyong.dialog.ShowOrDismissDialog;
import com.zdsoft.blp.gongyong.imageTrans.CircleImage;
import com.zdsoft.blp.gongyong.isLoginSuccess.IsLoginSuccess;
import com.zdsoft.blp.gongyong.myFragment.CustomFragment;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.zongbuju.MainActivity;
import com.zdsoft.blp.zongbuju.wode.activity.DengLuActivity;
import com.zdsoft.blp.zongbuju.wode.activity.GeRenXinXiActivity;
import com.zdsoft.blp.zongbuju.wode.activity.GuanYuWoMenActivity;
import com.zdsoft.blp.zongbuju.wode.activity.SheZhiActivity;
import com.zdsoft.blp.zongbuju.wode.activity.WoDeDingDanActivity;
import com.zdsoft.blp.zongbuju.wode.activity.WoDeFaBuActivity;
import com.zdsoft.blp.zongbuju.wode.activity.WoDeLiuYanActivity;
import com.zdsoft.blp.zongbuju.wode.activity.WoDeXiaoQuActivity;
import com.zdsoft.blp.zongbuju.wode.adapter.GrzxAdapter;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by pc on 2016/8/30.
 */
public class MyFragment extends CustomFragment implements NetCallback{

    private ListView lv_grzx;
    private List<Map<String, Object>> list;
    private LinearLayout ll_grxinxi;
    private ImageView iv_touxiang;
    private TextView tv_nickname, tv_phoneNumber;
    private SharedPreferences sp;
    private static final Integer HUOQUINFO =1;
    @Override
    protected void init() {
        initLayout(R.layout.wode);

        lv_grzx = find(R.id.lv_grzx);
        iv_touxiang = find(R.id.iv_touxiang);
        tv_nickname = find(R.id.tv_nickname);
        tv_phoneNumber = find(R.id.tv_phoneNumber);
        ll_grxinxi = find(R.id.ll_grxinxi);
        sp = getActivity().getSharedPreferences("info", Context.MODE_PRIVATE);

        GrzxAdapter adapter = new GrzxAdapter(getData(), getActivity());

        lv_grzx.setAdapter(adapter);

        lv_grzx.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String title = getData().get(position).get("title").toString();
                Intent intent;
                switch (title) {
                    case "我的订单":
                        if (IsLoginSuccess.isloginSuccess(getActivity(),true)){
                            intent = new Intent(getActivity(), WoDeDingDanActivity.class);
                            startActivity(intent);
                        }
                        break;
                    case "我的发布":
                        if (IsLoginSuccess.isloginSuccess(getActivity(),true)){
                             intent = new Intent(getActivity(), WoDeFaBuActivity.class);
                            startActivity(intent);
                        }
                        break;
                    case "我的小区":
                        if (IsLoginSuccess.isloginSuccess(getActivity(),true)){
                             intent = new Intent(getActivity(), WoDeXiaoQuActivity.class);
                            startActivity(intent);
                        }
                        break;
                    case "我的留言":
                        if (IsLoginSuccess.isloginSuccess(getActivity(),true)){
                            intent = new Intent(getActivity(), WoDeLiuYanActivity.class);
                            startActivity(intent);
                        }
                        break;
                    case "设置":
                        intent = new Intent(getActivity(), SheZhiActivity.class);
                        startActivity(intent);
                        break;
                    case "关于我们":
                        intent = new Intent(getActivity(), GuanYuWoMenActivity.class);
                        startActivity(intent);
                        break;
                }

            }
        });
        ll_grxinxi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = null;
                if (sp.getBoolean("isLoginSuccess", false)) {
                    intent = new Intent(getActivity(), GeRenXinXiActivity.class);
                    startActivity(intent);
                } else {
                    intent = new Intent(getActivity(), DengLuActivity.class);
                    startActivity(intent);
                }
            }
        });
    }
    public List<Map<String, Object>> getData() {
        list = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        map1.put("image", R.drawable.wd_dd);
        map1.put("title", "我的订单");
        list.add(map1);
        Map<String, Object> map2 = new HashMap<>();
        map2.put("image", R.drawable.wd_fb);
        map2.put("title", "我的发布");
        list.add(map2);
        Map<String, Object> map3 = new HashMap<>();
        map3.put("image", R.drawable.wd_xq);
        map3.put("title", "我的小区");
        list.add(map3);
        Map<String, Object> map4 = new HashMap<>();
        map4.put("image", R.drawable.wd_ly);
        map4.put("title", "我的留言");
        list.add(map4);
        Map<String, Object> map5 = new HashMap<>();
        map5.put("image", R.drawable.wd_sz);
        map5.put("title", "设置");
        list.add(map5);
        Map<String, Object> map6 = new HashMap<>();
        map6.put("image", R.drawable.gywm_03);
        map6.put("title", "关于我们");
        list.add(map6);
        return list;
    }

    public void getUserInfo() {
        if(sp.getBoolean("isLoginSuccess",false)){
            String  userId =sp.getString("userId","no");
            if(!"no".equals(userId)){
                Net net = new Net();
                net.url= API.MEMBER_URL;
                net.nameSpace= API.MEMBER_NAMESPACE;
                net.methodName = API.MEMBERINFO;
                Map<String,Object> map = new HashMap<>();
                map.put("userId", userId);
                net.parms =map;
                net.code=HUOQUINFO;
                ((MainActivity)getActivity()).getServer(net,this);
            }
        }else {
            tv_nickname.setText("您还未登录，登录");
            tv_phoneNumber.setVisibility(View.GONE);
            iv_touxiang.setImageResource(R.drawable.wd_del);
        }
    }


    @Override
    public void onStart() {
        super.onStart();
        getUserInfo();
    }

    @Override
    public void onResponse(boolean flag, int code, String res) {
        if(flag&&code==HUOQUINFO){
            try {
                JSONObject object = new JSONObject(res);
                String nickName =object.getString("nickName");
                String account = object.getString("account");
                String imageurl = object.getString("headImg");
                tv_nickname.setText(nickName);
                tv_phoneNumber.setVisibility(View.VISIBLE);
                tv_phoneNumber.setText("手机号:" + account);
                if(!"".equals(imageurl)&&imageurl!=null){
                    Picasso.with(getActivity()).load(imageurl).error(R.drawable.wd_del).transform(new CircleImage()).placeholder(R.drawable.wd_del).into(iv_touxiang);
                }else {
                    Picasso.with(getActivity()).load(R.drawable.wd_del).into(iv_touxiang);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        ShowOrDismissDialog.dismiss();
    }

}